package com.wangtao.netty.dubborpc.netty;

import com.wangtao.netty.dubborpc.provider.HelloServerImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        System.out.println("msg="+msg);
        //客户端调用服务器api时定义一个协议
        if (msg.toString().startsWith("HelloService#hello#")) {
            String result = new HelloServerImpl().sayHello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1
            ));
            ctx.writeAndFlush(result);
        }
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println(cause.getMessage());
        ctx.close();
    }
}
